package com.company.project.service.impl;

import com.company.project.dao.AdviserMapper;
import com.company.project.dao.AdviserResumeMapper;
import com.company.project.model.Adviser;
import com.company.project.model.AdviserResume;
import com.company.project.service.AdviserService;
import com.company.project.core.AbstractService;
import com.company.project.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


/**
 * Created by CodeGenerator on 2018/10/08.
 */
@Service
@Transactional
public class AdviserServiceImpl extends AbstractService<Adviser> implements AdviserService {
    @Resource
    @Autowired
    private AdviserMapper adviserMapper;
    @Resource
    private AdviserResumeMapper adviserResumeMapper;

    //获取全部顾问，分页
    @Override
    public PageUtil<Adviser> getAllAdviser(Integer pageIndex) {
        PageUtil<Adviser> pageUtil = new PageUtil();
        pageUtil.setCpage(pageIndex);
        pageUtil.setPageSize(6);
        pageUtil.setTotalRow(this.receptionCount());
        pageUtil.setLists(adviserMapper.selectAllAdviser(pageUtil));
        return pageUtil;
    }

    @Override
    public Integer receptionCount() {
        return adviserMapper.receptionCount();
    }


    //获取总条数
    @Override
    public Integer getAllCount() {
        return adviserMapper.selectAllCount();
    }

    /**
     * 更新顾问
     * @param adviser
     * @return
     */
    @Override
    public Boolean updateAdviser(Adviser adviser) {
        if (adviser.getStatus().equals(false)){
            adviserMapper.stateWithRank(adviser.getRank());
            adviser.setRank(-1);
            adviserMapper.AdjustRank(adviser);
        }else {
            if(this.rankCount(adviser.getRank())!=0){
                adviserMapper.upRank(adviser.getRank());
                adviserMapper.AdjustRank(adviser);
            }
        }
        adviserMapper.AdjustRank(adviser);
        Boolean aBoolean = adviserResumeMapper.deleteA(adviser);
        Boolean aBoolean1 = true;
        if (null != adviser.getAdviserResume() && adviser.getAdviserResume().size()>0){
            aBoolean1 = adviserResumeMapper.insertAR(adviser);
        }
        Boolean aBoolean2 = adviserMapper.updateAdviser(adviser);
        if (aBoolean.equals(true)&& aBoolean1.equals(true) && aBoolean2.equals(true)){
            return true;
        }
        return  false;
    }

    @Override
    public Integer rankCount(Integer rank) {
        return adviserMapper.rankCount(rank);
    }

    @Override
    public Boolean upRank(Integer rank) {
        return adviserMapper.upRank(rank);
    }

    /**
     * 调整专家顺序
     */
    @Override
    public Boolean AdjustRank(Adviser adviser) {
        Boolean aBoolean1 = adviserMapper.AdjustRank(adviser);
        return aBoolean1;
    }

    /**
     * 添加顾问
     * @param adviser
     * @return
     */
    @Override
    public Boolean addAdviser(Adviser adviser) {
        if (adviser.getStatus().equals(false)){
            adviser.setRank(-1);
        }else {
            if(this.rankCount(adviser.getRank())!=0){
                adviserMapper.upRank(adviser.getRank());
                adviserMapper.addAdviser(adviser);
            }
        }
        Boolean aBoolean1 = adviserMapper.addAdviser(adviser);
        if (adviser.getAdviserResume().size()>0){
            Boolean aBoolean2 = adviserResumeMapper.insertAR(adviser);
            if (aBoolean1.equals(true) && aBoolean2.equals(true)){
                return true;
            }
        }
        return aBoolean1;
    }

    @Override
    public Integer RankNum() {
        return adviserMapper.RankNum();
    }

    /**
     * 删除顾问
     * @return
     */
    @Override
    public Boolean deleteAdviser(Integer id) {
        if (this.selectRankById(id) <0){
            Boolean aBoolean1 = adviserResumeMapper.deleteAR(id);
            Boolean aBoolean = adviserMapper.deleteAdviser(id);
            if (aBoolean1.equals(true) && aBoolean.equals(true)){
                return true;
            }
        }
        Boolean aBoolean2 = this.stateWithRank(this.selectRankById(id));
        Boolean aBoolean1 = adviserResumeMapper.deleteAR(id);
        Boolean aBoolean = adviserMapper.deleteAdviser(id);
        if (aBoolean.equals(true) && aBoolean1.equals(true) && aBoolean2.equals(true)){
            return true;
        }
        return false;
    }

    @Override
    public Integer selectRankById(Integer id) {
        return adviserMapper.selectRankById(id);
    }

    /**
     * 后台查询顾问
     * @param pageIndex
     * @return
     */
    @Override
    public PageUtil<Adviser> selectAdminAdviser(Integer pageIndex) {
        PageUtil<Adviser> pageUtil = new PageUtil();
        pageUtil.setCpage(pageIndex);
        pageUtil.setPageSize(15);
        pageUtil.setTotalRow(this.getAllCount());
        pageUtil.setLists(adviserMapper.selectAdminAdviser(pageUtil));
        return pageUtil;
    }

    /**
     * 后台查询顾问详情
     * @return
     */
    @Override
    public List<Adviser> selectAdviserDetails(Integer id) {
        List<Adviser> s  = adviserMapper.selectAdviserDetails(id);
        return adviserMapper.selectAdviserDetails(id);
    }

    /**
     *前台专家轮播图
     * @return
     */
    @Override
    public List<Adviser> selectPicture() {
        return adviserMapper.selectPicture();
    }

    @Override
    public Boolean stateWithRank(Integer rank) {
        return adviserMapper.stateWithRank(rank);
    }

    /**
     * 前台查询专家详情
     * @param id
     * @return
     */
    @Override
    public List<Adviser> particularsById(Integer id) {
        return adviserMapper.particularsById(id);
    }


}
